package com.dyz.leetcode._14_binarySearchTree;

import com.dyz.leetcode.TreeNode;

public class _99_recoverTree {
    private TreeNode prev;
    private TreeNode x = null;
    private TreeNode y = null;
    public void recoverTree(TreeNode root) {
        dfs(root);
        int tmp = x.val;
        x.val = y.val;
        y.val = tmp;

    }
    private void dfs(TreeNode node){
        if(node==null){return;}

        dfs(node.left);
        if(prev!=null && node.val<prev.val){
            // node 不符合 BST 性质
            y = node;
            if(x==null){
                x= prev;
            }else {
                return;
            }
        }
        prev = node;
        dfs(node.right);
    }
}
